package com.googlecode.astoolkit.modules.customContextMenu {
	
	import com.googlecode.astoolkit.FrankyApi;
	import com.googlecode.astoolkit.utils.AbstractNode;
	import com.googlecode.astoolkit.utils.Sitemap;
	
	import flash.display.Sprite;
	import flash.events.ContextMenuEvent;
	import flash.ui.ContextMenu;
	import flash.ui.ContextMenuItem;


	public class CustomContextMenu {

		private var _data:Array;

		public function CustomContextMenu(tg:Sprite) {
			_data = Sitemap.nodes;
			var menuStage:ContextMenu = new ContextMenu();
			menuStage.hideBuiltInItems();
			for each(var item:AbstractNode in _data) {
				if (item.showInContextMenu == true) {
					var itemMenu:ContextMenuItem = new ContextMenuItem(item.title);
					itemMenu.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, contextMenuHandler);
					menuStage.customItems.push(itemMenu);
				}
			}
			tg.contextMenu = menuStage;
		}


		private function contextMenuHandler(event:ContextMenuEvent):void {
			FrankyApi.goto(getLink(ContextMenuItem(event.target).caption));
		}

		private function getLink(title:String):String {
			for each(var item:Object in _data) {
				if (item.title == title) {
					var link:String = item.path;
					break;
				}
			}
			return link;
		}


	}
}